/*
 * @FileName: bsp_pwm.h
 * @Author: GreyQiu qiushaogui@aikosolar.com
 * @Date: 2023-07-12 16:05:59
 * @LastEditors: GreyQiu qiushaogui@aikosolar.com
 * @LastEditTime: 2023-09-05 13:42:00
 * @Description: 
 * 
 * Copyright (c) 2023, All Rights Reserved. 
 */

#ifndef __BSP_PWM_H__
#define __BSP_PWM_H__

#ifdef __cplusplus
extern "C" {
#endif

#include "driverlib.h"

/**
 * @description PWM相关宏
 */
#define TB_CLK_FREQ             (100e6)      // 100mhz
#define PWM_FREQ                (100e3)      // 100Khz
#define PWM_PERIOD_CNT          (TB_CLK_FREQ / PWM_FREQ / 2)  // 计数器重加载值 中央对齐模式ARR值减一半

#define DEAD_TIME_H             32
#define DEAD_TIME_L             32

#define PWM_DUTY_CNT_START      (0) // (PWM_PERIOD_CNT / 2)  // 计数器重加载值 中央对齐模式ARR值减一半

#define PWM_BASE_PV1    EPWM1_BASE
#define PWM_BASE_PV2    EPWM2_BASE
#define PWM_BASE_BAT1   EPWM3_BASE
#define PWM_BASE_BAT2   EPWM4_BASE

/**
 * @description: 使能PWM引脚输出
 * @return 无
 */
static inline void bsp_pwm_output_enable(uint32_t pwm_base)
{
    EPWM_clearTripZoneFlag(pwm_base, EPWM_TZ_FLAG_OST); // 软件清除OST
//    EPWM_clearTripZoneFlag(pwm_base, EPWM_TZ_FLAG_OST);
}


/**
 * @description: 禁止PWM引脚输出
 * @return 无
 */
static inline void bsp_pwm_output_disable(uint32_t pwm_base)
{
    EPWM_forceTripZoneEvent(pwm_base, EPWM_TZ_FORCE_EVENT_OST);// 软件强制触发OST，EPWMxA和EPWMxB引脚默认置0
//    EPWM_forceTripZoneEvent(pwm_base, EPWM_TZ_FORCE_EVENT_OST);
}

/**
 * @description: 设置PWM周期
 * @param {uint16_t} period 周期宽度
 * @return 无
 */
static inline void bsp_pwm_period_set(uint32_t pwm_base, uint16_t periodCount)
{
    EPWM_setTimeBasePeriod(pwm_base, periodCount);
}

/**
 * @description: 设置PWM占空比
 * @param {uint16_t} duty 脉冲宽度
 * @return 无
 */
static inline void bsp_pwm_duty_set(uint32_t pwm_base, uint16_t dutyCount)
{
    EPWM_setCounterCompareValue(pwm_base, EPWM_COUNTER_COMPARE_A, dutyCount);
}

//============================================================
//函数申明区   外部调用
void bsp_pwm_init(void);
bool bsp_pwm_is_break(uint32_t base);
void bsp_pwm_break_clear(uint32_t base);

#ifdef __cplusplus
}
#endif

#endif 
